using System;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

public class WindowMessageNickName : MonoBehaviour
{
	private string[] ruFirstName;

	private string[] enFirstName;

	private string[] ruLastName;

	private string[] enLastName;

	private string stringForRuFirstName = "Крутой\nМощный\nНепобедимый\nМогучий\nКрепкий\nХитрый\nМрачный\nСуровый\nОпасный\nПугающий\nОтважный\nТурбо\nБезумный\nУникальный\nБожественный\nТаинственный\nАгрессивный\nВеликий\nЖестокий\nДикий\nБессмертный\nСправедливый\nНепревзойденный\nБезбашенный\nЖуткий\nВеликий\nБеспощадный\nСумасшедший\nСерьезный\nМатерый\nСумрачный\nДерзкий\nСтремительный\nВластный\nВосставший\nФантастический\nАлчный\nРазящий\nСмертельный\n";

	private string stringForRuLastName = "Бро\nБандит\nГанстер\nЧувак\nГоловорез\nСорвиголова\nМиротворец\nБарон\nМистер\nГосподин\nДжентельмен\nНаемник\nВояка\nСолдат\nКоммандос\nНезнакомец\nКрушитель\nПодрывник\nКостолом\nШпион\nСтранник\nГерой\nВоришка\nМясник\nРейнджер\nГений\nЗлодей\nКойот\nВолк\nЯстреб\nМогильщик\nПризрак\nСтрелок\nЧереп\nМститель\nФантом\nАннигилятор\nПсих\nКочевник\n";

	private string stringForEnFirstName = "Cool\nStrong\nTough\nSevere\nElusive\nInvincible\nHot\nMighty\nBrave\nTricky\nGlourious\nGloomy\nDark\nCrazy\nGenial\nGreat\nShadow\nMiracle\nAgressive\nWild\nUndead\nMysterious\nHappy\nPoison\nSteel\nHeavy\nSoul\nFair\nHeavy\nDangerous\nInterrupt\nGreat\nElectric\nImpetuous\nFantastic\nAmazing\nSmashing\nFatal\nSoaring\n";

	private string stringForEnLastName = "Bro\nBandit\nGansta\nHomie\nWalker\nStranger\nThug\nGunman\nGuy\nMan\nDude\nPeacemaker\nHero\nBaron\nMister\nJourneyman\nSomebody\nHunter\nWarrior\nSpy\nBomber\nHummer\nBlade\nBreaker\nStealer\nDigger\nGhost\nShooter\nSkull\nEagle\nAvenger\nPhantom\n";

	private int maxChars = 19;

	private TouchScreenKeyboard mKeyboard;

	private string mText = string.Empty;

	public static WindowMessageNickName inst;

	public UILabel nicknameLabel;

	public UILabel myNickName;

	public GameObject windowNickName;

	public GameObject yesButton;

	public UILabel noButton;

	public bool hideWindows = true;

	public GameObject random;

	public GameObject buttonChange;

	public GameObject bottonYes;

	public GameObject youreRandomNickName;

	public GameObject labelNickName;

	public GameObject activateBoxCollider;

	public GameObject nickName;

	public GameObject buttonRdy;

	public GameObject labelLikeNickName;

	public GameObject labelError;

	private string curNickName;

	public static event Action buttonNoChange;

	private void Awake()
	{
		inst = this;
		WriteAllMass();
		curNickName = BCWStorage.Get<string>(settings.keyRandomNickName);
		if (string.IsNullOrEmpty(curNickName))
		{
			curNickName = GetRandomNickName();
			BCWStorage.Set(settings.keyRandomNickName, curNickName);
		}
	}

	private void Start()
	{
		string text = BCWStorage.Get<string>(settings.keyCryptoName);
		if ((text == "Player" || text == string.Empty || settings.tekName.Length > 19) && SceneManager.GetActiveScene().name != "Tutorial")
		{
			nicknameLabel.text = GetRandomNickName();
			windowNickName.SetActive(true);
		}
	}

	public void Init()
	{
		nicknameLabel.text = curNickName;
		windowNickName.SetActive(true);
	}

	public void WriteAllMass()
	{
		ruFirstName = stringForRuFirstName.Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
		enFirstName = stringForEnFirstName.Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
		ruLastName = stringForRuLastName.Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
		enLastName = stringForEnLastName.Split(new char[1] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
	}

	public void buttonRandom()
	{
		BCWStorage.Set(settings.keyRandomNickName, GetRandomNickName());
		nicknameLabel.text = BCWStorage.Get<string>(settings.keyRandomNickName);
	}

	public string GetRandomNickName()
	{
		if (LocalizationManager.IsRussian())
		{
			return CreateName(19, ruFirstName, ruLastName);
		}
		return CreateName(19, enFirstName, enLastName);
	}

	private string CreateName(int maxLength, string[] firstNames, string[] lastNames)
	{
		string text = string.Empty;
		string text2 = string.Empty;
		for (int i = 0; i < 7; i++)
		{
			text = firstNames[UnityEngine.Random.Range(0, firstNames.Length)];
			text2 = lastNames[UnityEngine.Random.Range(0, lastNames.Length)];
			if ((text + text2).Length < maxLength)
			{
				break;
			}
			if (i == 6)
			{
				text = firstNames[0];
				text2 = firstNames[0];
			}
		}
		return (text + " " + text2).Trim();
	}

	public void ClickButtonCreateNewNick()
	{
		buttonRdy.GetComponent<BoxCollider>().enabled = false;
		buttonRdy.GetComponent<UIButton>().SetState(UIButtonColor.State.Disabled, false);
		random.SetActive(false);
		buttonChange.SetActive(false);
		bottonYes.SetActive(false);
		youreRandomNickName.SetActive(false);
		labelNickName.SetActive(false);
		nickName.SetActive(true);
		buttonRdy.SetActive(true);
		activateBoxCollider.GetComponent<BoxCollider>().enabled = true;
		labelLikeNickName.SetActive(true);
		myNickName.text = string.Empty;
		mKeyboard = TouchScreenKeyboard.Open(string.Empty, TouchScreenKeyboardType.Default, false, false);
	}

	public void ClickButtonNotChange()
	{
		windowNickName.SetActive(false);
		settings.setNewName(nicknameLabel.text);
		hideWindows = true;
		if (TutorialManager.TuturialWorking())
		{
			WindowMessageNickName.buttonNoChange();
		}
	}

	public void OpenKeyboard()
	{
		mKeyboard = TouchScreenKeyboard.Open(string.Empty, TouchScreenKeyboardType.Default, false, false);
		labelError.SetActive(false);
		myNickName.gameObject.SetActive(true);
		myNickName.text = string.Empty;
	}

	private void CheckNickName()
	{
		if ((myNickName.text == string.Empty || myNickName.text[0].ToString() == " " || myNickName.text == "Player") && myNickName.gameObject.GetActive())
		{
			labelError.SetActive(true);
			myNickName.gameObject.SetActive(false);
		}
	}

	public void ClickRdy()
	{
		windowNickName.SetActive(false);
		settings.setNewName(myNickName.text);
		hideWindows = true;
		if (TutorialManager.TuturialWorking())
		{
			WindowMessageNickName.buttonNoChange();
		}
	}

	public void ClickBack()
	{
		buttonRdy.GetComponent<BoxCollider>().enabled = false;
		buttonRdy.GetComponent<UIButton>().SetState(UIButtonColor.State.Disabled, false);
		activateBoxCollider.GetComponent<BoxCollider>().enabled = false;
		random.SetActive(true);
		buttonChange.SetActive(true);
		bottonYes.SetActive(true);
		youreRandomNickName.SetActive(true);
		labelNickName.SetActive(true);
		nickName.SetActive(false);
		buttonRdy.SetActive(false);
		labelLikeNickName.SetActive(false);
		labelError.SetActive(false);
		mKeyboard.active = false;
	}

	private void Update()
	{
		if (mKeyboard == null)
		{
			return;
		}
		string text = mKeyboard.text;
		if (mText != text)
		{
			mText = string.Empty;
			foreach (char c in text)
			{
				if (c != 0)
				{
					mText += c;
				}
			}
			if (maxChars > 0 && mKeyboard.text.Length > maxChars)
			{
				mKeyboard.text = mKeyboard.text.Substring(0, maxChars);
			}
			if (text != string.Empty && text[0].ToString() != " " && text != "Player")
			{
				buttonRdy.GetComponent<BoxCollider>().enabled = true;
				buttonRdy.GetComponent<UIButton>().SetState(UIButtonColor.State.Normal, false);
			}
			else
			{
				buttonRdy.GetComponent<BoxCollider>().enabled = false;
				buttonRdy.GetComponent<UIButton>().SetState(UIButtonColor.State.Disabled, false);
			}
			if (mText != text)
			{
				mKeyboard.text = mText;
			}
		}
		myNickName.text = mText;
		if (mKeyboard.done && !mKeyboard.wasCanceled)
		{
			CheckNickName();
		}
		else if (mKeyboard.wasCanceled)
		{
			CheckNickName();
		}
		if (mKeyboard != null && !mKeyboard.active)
		{
			CheckNickName();
		}
	}
}
